﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ShareMarking.Entity;
using ShareMarking.Service;

namespace ShareMarking.Interface
{
    public class Qrcode_vigo
    {
        //var httpstr = "http://my.erpcoo.com/index.php/Api/Qrcode/index";
        //var httpstr = "http://test.erpcoo.cn/index.php/Api/Qrcode/index";

        protected string httpstr;
        protected string signserect;

        public Qrcode_vigo(string httpstr, string signserect)
        {
            this.httpstr = httpstr;
            this.signserect = signserect;
        }

        public result CreateQrcode(message message)
        {
            #region sign
            var timestamp = DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss");

            var parameters = new Dictionary<string, string>();
            parameters.Add("message", ComLib.JsonHelper.JsonSerializer<message>(message));
            parameters.Add("method", "001");
            parameters.Add("timestamp", timestamp);

            var request_str = BuildQuery(parameters) + this.signserect;
            var signature = ComLib.DEncrypt.MD5ForPHP(request_str.ToLower());
            parameters.Add("sign", signature);
            #endregion

            var pst = BuildQuery2(parameters).ToString();
            var postData = Encoding.UTF8.GetBytes(pst);
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string result = click.Post(this.httpstr, postData);
            var resultentity= ComLib.JsonHelper.JsonDeserialize<ShareMarking.Entity.result>(result);
            
            return resultentity;
        }

        public string UpdateQrcode(message2 message)
        {
            #region sign
            var timestamp = DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss");
            var parameters = new Dictionary<string, string>();
            parameters.Add("message", ComLib.JsonHelper.JsonSerializer<message2>(message));
            parameters.Add("method", "002");
            parameters.Add("timestamp", timestamp);

            var request_str = BuildQuery(parameters) + this.signserect;
            var signature = ComLib.DEncrypt.MD5ForPHP(request_str.ToLower());
            parameters.Add("sign", signature);
            #endregion

            var postData = Encoding.UTF8.GetBytes(BuildQuery2(parameters));
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string result = click.Post(this.httpstr, postData);
            //var resultentity = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Entity.result>(result);
            //if (resultentity.errcode == "00")
            //{
            //    qr_QrCode qrcode = qr_QrCodeService.Instance.Get_SingleEntity_byWhere(qr_QrCode._.id==611);
                
            //}
            return result;
        }

        public result_qrlog GetUsedCount(int page)
        {
            #region sign
            var timestamp = DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss");
            var parameters = new Dictionary<string, string>();
            parameters.Add("message", "{\"startdate\":\"" + "2016-03-0512:05:00" 
                + "\",\"enddate\":\"" + "2016-05-2000:00:00"
                 + "\",\"pageno\":\"" + page
                  + "\",\"pagesize\":\"" + "2000"
                  + "\"}");
            parameters.Add("method", "003");
            parameters.Add("timestamp", timestamp);

            var request_str = BuildQuery(parameters) + this.signserect;
            var signature = ComLib.DEncrypt.MD5ForPHP(request_str.ToLower());
            parameters.Add("sign", signature);
            #endregion

            var pst = BuildQuery2(parameters).ToString();
            var postData = Encoding.UTF8.GetBytes(pst);
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string result = click.Post(this.httpstr, postData);
            var resultentity = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Entity.result_qrlog>(result);

            return resultentity;
        }

        public resultca GetWXContent(string code, string scene_id)
        {
            #region sign
            var timestamp = DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss");

            var parameters = new Dictionary<string, string>();
            parameters.Add("message", "{\"code\":\"" + code + "\",\"scene_id\":\"" + scene_id + "\"}");
            parameters.Add("method", "004");
            parameters.Add("timestamp", timestamp);

            var request_str = BuildQuery(parameters) + this.signserect;
            var signature = ComLib.DEncrypt.MD5ForPHP(request_str.ToLower());
            parameters.Add("sign", signature);
            #endregion

            var pst = BuildQuery2(parameters).ToString();
            var postData = Encoding.UTF8.GetBytes(pst);
            ComLib.WebClientHelper click = new ComLib.WebClientHelper();
            click.Encoding = Encoding.UTF8;
            string result = click.Post(this.httpstr, postData);
            var resultentity = ComLib.JsonHelper.JsonDeserialize<ShareMarking.Entity.resultca>(result);

            return resultentity;
        }

        /// <summary>
        /// 组装普通文本请求参数。
        /// </summary>
        /// <param name="parameters">Key-Value形式请求参数字典</param>
        /// <returns>URL编码后的请求数据</returns>
        public static string BuildQuery(IDictionary<string, string> parameters)
        {
            StringBuilder postData = new StringBuilder();

            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    postData.Append(name);
                    if (name == "message")
                    {
                        postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
                    }
                    else
                    {
                        postData.Append(value);
                    }
                }
            }
            return postData.ToString();
        }

        /// <summary>
        /// 组装普通文本请求参数。
        /// </summary>
        /// <param name="parameters">Key-Value形式请求参数字典</param>
        /// <returns>URL编码后的请求数据</returns>
        public static string BuildQuery2(IDictionary<string, string> parameters)
        {
            StringBuilder postData = new StringBuilder();
            bool hasParam = false;

            IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
            while (dem.MoveNext())
            {
                string name = dem.Current.Key;
                string value = dem.Current.Value;
                // 忽略参数名为空的参数
                if (!string.IsNullOrEmpty(name))
                {
                    if (hasParam)
                    {
                        postData.Append("&");
                    }

                    postData.Append(name);
                    postData.Append("=");
                    //if (name == "message")
                    //{
                    //    postData.Append(HttpUtility.UrlEncode(value));
                    //}
                    //else
                    //{
                        postData.Append(value);
                    //}
                    hasParam = true;
                }
            }

            return postData.ToString();
        }
    }
}
